<template>
  <div class="container">
    <div class="app-container">
      组织架构
      <el-tree :data="depts" :props="defaultProps" :default-expanded-all="true" :expand-on-click-node="true">
        <template v-slot="{ data }">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event 实参 表示类型 -->
              <el-dropdown @command="operateDept($event, data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 添加子部门 -->
    <add_dept ref="depart" :is-edit="isEdit" :is-show.sync="isShow" :pid="pid" @Department="getDepartmentList" />
  </div>
</template>
<script>
import { getDepartment, removeDepartment } from '@/api/department'
import add_dept from './components/add_dept.vue'
import { newArr } from '@/utils'
export default {
  name: 'Department',
  components: {
    add_dept
  },
  data() {
    return {
      pid: 0,
      isShow: false,
      isEdit: false, // 弹框头部显示
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    async getDepartmentList() {
      const res = await getDepartment()
      console.log(res)
      const departmentRes = newArr(res.data, 0)
      this.depts = departmentRes
    },
    operateDept(type, id) {
      this.pid = id
      if (type === 'add') {
        this.isShow = true
        this.isEdit = false
      }
      if (type === 'edit') {
        this.isShow = true
        this.isEdit = true
        // 父传子,可以看作是一个异步的任务,不能在第一时间就获取到值
        // 想要第一时间拿到值一定要使用$nextTick()进行获取
        // $nextTick专门用于处理异步请求
        this.$nextTick(() => {
          // 让子组件去发请求拿到详情数据
          this.$refs.depart.getDetail()
        })
      }
      if (type === 'del') {
        this.$confirm('确定删除?').then(() => {
          this.handleDel(id)
        }).catch(() => {})
      }
    },
    async handleDel(id) {
      const res = await removeDepartment(id)
      console.log(res)
      this.$message.success('删除成功')
      // 更新数据
      this.getDepartmentList()
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>
